package com.ccp.dev.system.controller;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.*;
import com.ccp.dev.system.service.MessageReadService;
import com.ccp.dev.system.service.MessageReplyService;
import com.ccp.dev.system.service.MessageSendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 对象功能:已读信息者 控制器类
 * 开发人员:dxy
 * 创建时间:2019-08-01 15:14:52
 */
@Controller
@Action(ownermodel= SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM + "/messageRead")
public class MessageReadController extends SysBaseController {
	@Resource
	private MessageReadService readService;
	@Resource
	private MessageSendService sendService;
	@Resource
	private MessageReplyService replyService;
	private static final String SUCCESS = "operation.success";
	private static final String FAILURE = "operation.failure";

	/**
	 * 取得接收状态分页列表
	 * @param request
	 * @param modelMap
	 * @return ResultData
	 */
	@RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
	@Action(description="查看接收信息列表",detail="查看接收信息列表<#if success>成功<#else>失败</#if>",exectype = "查询日志")
	@ResponseBody
	public ResultData list(HttpServletRequest request, ModelMap modelMap) {
		SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
		String userId = curUser.getUserId();
		String returnUrl = RequestUtil.getPrePage(request);
		String messageId = RequestUtil.getString(request, "messageId");
		MessageSend messageSend = new MessageSend();
		messageSend = sendService.getById(messageId);
		QueryFilter queryFilter = new QueryFilter(request);
		queryFilter.addFilter("userId", userId);
		try {
			List<MessageReply> list = replyService.getAll(queryFilter);
			// 插入已读信息表
			readService.addMessageRead(messageId,curUser);
			modelMap.put("replyList", list);
			modelMap.put("userId", userId);
			modelMap.put("messageSend", messageSend);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess(SUCCESS, modelMap);
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError(FAILURE);
		}
	}

	/**
	 * 取得接收状态明细
	 * @param request
	 * @return ResultData
	 */
	@RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
	@Action(description="查看接收信息",detail="查看接收信息<#if success>成功<#else>失败</#if>",exectype = "查询日志")
	public ResultData get(HttpServletRequest request){
		String id = RequestUtil.getString(request, "id");
		try {
			MessageRead messageRead = readService.getById(id);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess(SUCCESS, messageRead);
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError(FAILURE);
		}
	}
	/*					                     页面跳转
	 ******************************************************************************************************************************/
	/**
	 * 取得接收状态明细
	 * @param request
	 * @param model
	 * @return html文件前缀
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
	@Action(description = "消息已读列表页面跳转", exectype = "页面跳转日志")
	public String runList(HttpServletRequest request, Model model){
		String messageId = RequestUtil.getString(request, "messageId");
		model.addAttribute("messageId", messageId);
		return "messageRead/form";
	}
}
